package cn.com.yang.egg;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;

import static java.lang.invoke.MethodHandles.lookup;

/**
 * @Desc<方法句柄  JSR292>
 * @Author 杨亿金
 * @Date 2021/1/18 9:48
 * @CopyRight
 * @Version
 */
public class EggMethodHandle {
    static class ClassA{
        public void println(String s){
            System.out.println(s);
        }
    }

    private static MethodHandle getPrintlnMH(Object receiver) throws Exception{
        MethodType mt = MethodType.methodType(Void.class,String.class);
        return lookup().findVirtual(receiver.getClass(),"println",mt).bindTo(receiver);
    }
    public static void main(String[] args) throws Throwable {
        Object obj = System.currentTimeMillis()%2==0?System.out:new ClassA();
        getPrintlnMH(obj).invokeExact("hello");
    }
}

